from PIL import Image # Pillow  
import qrcode
from qrcode.util import QRData
import os

#https://www.cnblogs.com/sfnz/p/5457862.html

fileUrl = os.path.abspath(__file__)
urlArr = fileUrl.split(os.sep)
urlArr = urlArr[0:len(urlArr)-1]
folderUrl = os.sep.join(urlArr)
saveUrl = os.path.join(folderUrl,"qrcode1.png")

qr=qrcode.QRCode(
    version=2,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10, #控制二维码中每个小格子包含的像素数。
    border=4 #控制边框（二维码与图片边界的距离）包含的格子数（默认为4，是相关标准规定的最小值）
    )

data = "http://192.168.33.185/game/"
data = "http://127.0.0.1:5460/index.html"
data = "http://game.fire2333.com/home/ac?action=/home/game/a/1111/g/200042/pt/9311"
data = "http://39.108.14.222/Client/apk/扶摇H5_研发内测_20180707175342_v20180707.apk"
data = "http://192.168.32.12/apk_h5/app-release.apk"
data = "http://192.168.32.12/apk_h5/app-release.apk"
data = "http://192.168.0.53:8080"
data = "http://192.168.1.85/game/"
data = "http://39.108.14.222/Client/web/testphp/index.html"
data = "http://game.fire2333.com/home/ac?action=/home/game/a/1111/g/200042/pt/9311"
data = "http://game.fire2333.com/home/ac?action=/home/game/a/1111/g/200042/pt/9311"
data = "http://192.168.19.185/game/?gm=true&fps=true" #85
data = "http://192.168.23.128:8080/?v=1826&gm=true" #我自己
data ="http://testsw.yyxxgame.com/"
#data = "http://192.168.32.12:8080/index.html"
#data = "http://192.168.32.12/gameweb/HZRecorder.html"


#添加二维码头像
icon = Image.open(os.path.join(folderUrl,"qrcodeIco.png")) #qrcodeIco yy
qr.add_data(data)
qr.make(fit=True)

img = qr.make_image()
img=img.convert("RGBA") #设置RGBA通道 不然头像是黑白的
img_w,img_h=img.size
print("img_w,img_h",img_w,img_h)
factor=4
size_w=int(img_w/factor)
size_h=int(img_h/factor) 
print("size_w,size_h",size_w,size_h)
icon_w,icon_h=icon.size
if icon_w>size_w:
    icon_w=size_w
if icon_h>size_h:
    icon_h=size_h
icon=icon.resize((icon_w,icon_h)) 
#头像在二维码的位置信息
px=int((img_w-icon_w)/2)
py=int((img_h-icon_h)/2)
img.paste(icon,(px,py))

img.save(saveUrl)
